package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.LoginInfoUtil;
import cn.itsource.pet.domain.OrderPetAcquisition;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.OrderPetAcquisitionQuery;
import cn.itsource.pet.service.IOrderPetAcquisitionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
//Propagation.SUPPORTS:支持别人事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition, OrderPetAcquisitionQuery> implements IOrderPetAcquisitionService {

    @Autowired
    private SearchMasterMsgMapper msgMapper;

    @Autowired
    private OrderPetAcquisitionMapper opaMapper;

    @Override
    public AjaxResult jieDan(SearchMasterMsg msg) {

        //1.修改msg的状态
        msg.setShopId(LoginInfoUtil.getShopId());
        msgMapper.updateByPrimaryKeySelective(msg);

        //2.先查询
        SearchMasterMsg searchMasterMsg = msgMapper.selectByPrimaryKey(msg.getId());

        //2.拷贝数据到订单表
        OrderPetAcquisition opa = BeanUtil.copyProperties(searchMasterMsg, OrderPetAcquisition.class);
        opa.setDigest(msg.getNote());
        opa.init();
        opa.setEmpId(msg.getEmpId());
        opa.setMsgId(msg.getId());

        opaMapper.insertSelective(opa);

        return AjaxResult.createSuccess();
    }
}
